package com.wuh.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wuh.model.ArticleType;
import com.wuh.model.Collect;
import com.wuh.model.Comment;
import com.wuh.model.Read;
import com.wuh.model.Teacher;
import com.wuh.model.Toefl;
import com.wuh.model.User;
import com.wuh.service.ToeflService;

@Controller
public class ToeflController {

	@Autowired
    private ToeflService toeflService;
//	将数据库的内容分类以列表形式展现在托福人页面
	@RequestMapping("/toToefl")
	public String titlelist(Toefl toefl,HttpSession session) {
		List<Toefl> toe1 = toeflService.titlelist(toefl);
		session.setAttribute("sess_toefl", toe1);
		List<Toefl> toe2 = toeflService.titlelist2(toefl);
		session.setAttribute("sess_toefl2", toe2);
		List<Toefl> toe3 = toeflService.titlelist3(toefl);
		session.setAttribute("sess_toefl3", toe3);
		List<Toefl> toe4 = toeflService.titlelist4(toefl);
		session.setAttribute("sess_toefl4", toe4);
		List<Toefl> toe5 = toeflService.titlelist5(toefl);
		session.setAttribute("sess_toefl5", toe5);
		List<Toefl> toe6 = toeflService.titlelist6(toefl);
		session.setAttribute("sess_toefl6", toe6);
		List<Toefl> toe7 = toeflService.titlelist7(toefl);
		session.setAttribute("sess_toefl7", toe7);
		List<Toefl> toe8 = toeflService.titlelist8(toefl);
		session.setAttribute("sess_toefl8", toe8);
		return "toefl/toefl_main";
	}
	
//	点击指定文章类别的查看所有，转到当前类别所有文章界面
	@RequestMapping("/toToefl_classification")
	public String toToefl_classification(Toefl toefl,Integer activity_type,Model model,Integer pageNum,HttpSession session) {
		if (pageNum != null) {
			int a = (int) session.getAttribute("activity_type");
			toefl.setPageNum(pageNum);
			PageInfo<Toefl> toefl1=toeflService.getClassByat(toefl,a);
			model.addAttribute("sess_toeflclass", toefl1);
		}else {
			PageInfo<Toefl> toefl1=toeflService.getClassByat(toefl,activity_type);
			model.addAttribute("sess_toeflclass", toefl1);
			session.setAttribute("activity_type", activity_type);
		}
		return "toefl/toefl_classification";
	}
	
//	点击文章进入文章详细页面
	@RequestMapping("/toToefl_detail")
	public String toToefl_detail(Comment comment,Integer article_id,HttpSession session,Model model,Integer pageNum,Read read) {
		if (pageNum != null) {
			int a = (int) session.getAttribute("article_id");
			Toefl toefl=toeflService.getArticleByai(a);
			session.setAttribute("sess_toefldetail", toefl);
//			获取该文章评论数量
			int a1=toeflService.getCommentsumbyid(a);
			session.setAttribute("sess_sum", a1);
//			获取该文章用户评论
			comment.setPageNum(pageNum);
			PageInfo<Toefl> coment = toeflService.getComent(comment,a);
			model.addAttribute("sess_com", coment);
		}else {
			Toefl toefl=toeflService.getArticleByai(article_id);
			session.setAttribute("sess_toefldetail", toefl);
//			获取该文章评论数量
			int a=toeflService.getCommentsumbyid(article_id);
			session.setAttribute("sess_sum", a);
//			获取该文章用户评论
			PageInfo<Toefl> coment = toeflService.getComent(comment,article_id);
			model.addAttribute("sess_com", coment);
			session.setAttribute("article_id", article_id);
//			文章阅览记录
			String a1  =(String) session.getAttribute("loginname");
			if (a1 !=null) {		User user = toeflService.getIdByname(a1);
			Integer read_user = user.getUser_id();
			Date lm = new Date();
			toeflService.updateRead(article_id,read_user,lm,read);
			int sum = toeflService.getReadsum(article_id);
			toeflService.editRead(sum,article_id);
			}
		}
		return "toefl/toefl_detail";
	}
	
//	收藏
	@RequestMapping("/collect")
	public String collect(Integer article_id, HttpSession session,Collect collect,Model model,Comment comment) {
//		获取登录id，插入收藏信息
		String a  =(String) session.getAttribute("loginname");
		User user = toeflService.getIdByname(a);
		Integer collect_user = user.getUser_id();
		Date lm = new Date();
		toeflService.updateCollect(article_id,collect_user,lm,collect);
//		更新文章信息表收藏数字段
		int sum = toeflService.getCollectsum(article_id);
		toeflService.editArticle(sum,article_id);
		Toefl toefl=toeflService.getArticleByai(article_id);
		session.setAttribute("sess_toefldetail", toefl);
//		获取该文章评论数量
		int a1=toeflService.getCommentsumbyid(article_id);
		session.setAttribute("sess_sum", a1);
//		获取该文章用户评论
		PageInfo<Toefl> coment = toeflService.getComent(comment,article_id);
		model.addAttribute("sess_com", coment);
		session.setAttribute("article_id", article_id);
		return "toefl/toefl_detail";
	}
	
//	文章评价
	@RequestMapping("/comment")
	public String comment(HttpSession session,HttpServletRequest request,Model model,Comment comment) {
		String a  =(String) session.getAttribute("loginname");
		String desc = request.getParameter("comment");
		String b  = request.getParameter("article_id");
		String c  = request.getParameter("publish_teacher");
		Integer article_id = Integer.valueOf(b);
		Integer publish_teacher = Integer.valueOf(c);
		if (a != null) {
			User user = toeflService.getIdByname(a);
			Integer comment_user = user.getUser_id();
			Date lm = new Date();
			toeflService.updateComment(article_id,desc,comment_user,publish_teacher,lm);
//			获取该文章评论数量
			int a1=toeflService.getCommentsumbyid(article_id);
			session.setAttribute("sess_sum", a1);
//			获取该文章用户评论
			PageInfo<Toefl> coment = toeflService.getComent(comment,article_id);
			model.addAttribute("sess_com", coment);
			session.setAttribute("article_id", article_id);
		}else {
			 session.setAttribute("msg","请先登录再试");
		}
		return "toefl/toefl_detail";
	}
	
//	个人中心-我的托福人
	@RequestMapping("/toMyToefl")
	public String toMyToefl(HttpSession session,Model model,Integer pageNum,Toefl toefl,Integer flag) {
		String a  =(String) session.getAttribute("loginname");
		String b = (String) session.getAttribute("loginMessage");
		if (b.equals("user")) {
			if (pageNum != null) {
				toefl.setPageNum(pageNum);
				if (flag !=null) {
					User user = toeflService.getIdByname(a);
					Integer collect_user = user.getUser_id();
					PageInfo<Toefl> t= toeflService.collectListBycu(toefl,collect_user);
					model.addAttribute("myToefl", t);
					PageInfo<Toefl> t1 = toeflService.lastArticleList(toefl);
					model.addAttribute("lastToefl", t1);
					return "toefl/user_myToefl";
				}else {
					User user = toeflService.getIdByname(a);
					Integer collect_user = user.getUser_id();
					PageInfo<Toefl> t= toeflService.collectListBycu(toefl,collect_user);
					model.addAttribute("myToefl", t);
					PageInfo<Toefl> t1 = toeflService.lastArticleList(toefl);
					model.addAttribute("lastToefl", t1);
					return "person/user_myToefl";
				}
			}else {
//				收藏文章
				User user = toeflService.getIdByname(a);
				Integer collect_user = user.getUser_id();
				PageInfo<Toefl> t= toeflService.collectListBycu(toefl,collect_user);
				model.addAttribute("myToefl", t);
//				最近发布文章
				PageInfo<Toefl> t1 = toeflService.lastArticleList(toefl);
				model.addAttribute("lastToefl", t1);
				return "person/user_myToefl";
			}
		}else if (b.equals("teacher")) {
			if (pageNum != null) {
				toefl.setPageNum(pageNum);
				if (flag !=null) {
					Teacher teacher = toeflService.getIdBytname(a);
					Integer publish_teacher= teacher.getTeacher_id();
					PageInfo<Toefl> t1=toeflService.getArticleBytid(toefl,publish_teacher);
					model.addAttribute("myArticle", t1);
					PageInfo<Toefl> t11 = toeflService.lastArticleList(toefl);
					model.addAttribute("lastToefl", t11);
					return "toefl/teacher_myToefl";
				}else {
					Teacher teacher = toeflService.getIdBytname(a);
					Integer publish_teacher= teacher.getTeacher_id();
					PageInfo<Toefl> t1=toeflService.getArticleBytid(toefl,publish_teacher);
					model.addAttribute("myArticle", t1);
					PageInfo<Toefl> t11 = toeflService.lastArticleList(toefl);
					model.addAttribute("lastToefl", t11);
					return "teacher/teacher_myToefl";
				}
			}else {
				Teacher teacher = toeflService.getIdBytname(a);
				Integer publish_teacher= teacher.getTeacher_id();
				PageInfo<Toefl> t1=toeflService.getArticleBytid(toefl,publish_teacher);
				model.addAttribute("myArticle", t1);
				PageInfo<Toefl> t11 = toeflService.lastArticleList(toefl);
				model.addAttribute("lastToefl", t11);
				return "teacher/teacher_myToefl";
			}
		}
		return null; 
	}
	
	

//	发布文章页面跳转
	@RequestMapping("/topublish")
	public String topublish() {
		return "toefl/toefl_publish";
	}
	
//	发布文章
	@RequestMapping("/addAtricle")
	public String addAtricle(Toefl toefl,HttpSession session,HttpServletRequest request,MultipartFile myJpg) {
	        if (!myJpg.isEmpty()) {
	        	String fileDirPath = new String("src/main/resources/" + "static/toeflImg");
	        	File staticfile =new File(fileDirPath) ;
	        	String filename = myJpg.getOriginalFilename();
	        	int lastIndex = filename.lastIndexOf(".");
	        	String suffix = filename. substring(lastIndex);
	        	Date date = new Date();
	        	String newName = date.getTime()+ suffix;
	        	String uploadPath = staticfile. getAbsolutePath() +File.separator+newName;
	        	File targetFile = new File (uploadPath) ;
	        	try {
					myJpg.transferTo(targetFile);
					System.out.println("文件上传成功");
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        	String imgPath ="toeflImg" +"\\" +newName ;
	            //存储宣传图片地址
	            toefl.setImg_url(imgPath);
	            String a  =(String) session.getAttribute("loginname");
				Teacher teacher = toeflService.getIdBytname(a);
				toefl.setPublish_teacher(teacher.getTeacher_id());
				toefl.setLast_modify(new Date());
				toeflService.addAtricle(toefl);
	        } else {
	        	String a  =(String) session.getAttribute("loginname");
				Teacher teacher = toeflService.getIdBytname(a);
				toefl.setPublish_teacher(teacher.getTeacher_id());
				toefl.setLast_modify(new Date());
				toeflService.addAtricle(toefl);

	        }
	        return "redirect:/toMyToefl";
	}
	
//	删除文章
	@RequestMapping("/deleteArticle")
	public String deleteArticle(Integer article_id) {
//		删阅览
		toeflService.delArticleByid(article_id);
//		删收藏
		toeflService.delArticleByid1(article_id);
//		删评论
		toeflService.delArticleByid2(article_id);
//		删文章
		toeflService.delArticleByid3(article_id);
		return "redirect:/toMyToefl";
	}
	
//	编辑文章
	@RequestMapping("/editArticle")
	public String editArticle(Integer article_id,HttpSession session) {
		Toefl toefl = toeflService.getArticleByai(article_id);
		session.setAttribute("editArticle", toefl);
		return "redirect:/toedit";
	}
//	编辑文章页面跳转
	@RequestMapping("/toedit")
	public String toedit() {
		return "toefl/toefl_edit";
	}

//	发布文章
	@RequestMapping("/editAtricle")
	public String editAtricle(Toefl toefl,HttpSession session,HttpServletRequest request,MultipartFile myJpg) {
	        if (!myJpg.isEmpty()) {
	        	String fileDirPath = new String("src/main/resources/" + "static/toeflImg");
	        	File staticfile =new File(fileDirPath) ;
	        	String filename = myJpg.getOriginalFilename();
	        	int lastIndex = filename.lastIndexOf(".");
	        	String suffix = filename. substring(lastIndex);
	        	Date date = new Date();
	        	String newName = date.getTime()+ suffix;
	        	String uploadPath = staticfile. getAbsolutePath() +File.separator+newName;
	        	File targetFile = new File (uploadPath) ;
	        	try {
					myJpg.transferTo(targetFile);
					System.out.println("文件上传成功");
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        	String imgPath ="/toeflImg" +"\\" +newName ;
	            //存储宣传图片地址
	            toefl.setImg_url(imgPath);
	            String a  =(String) session.getAttribute("loginname");
				Teacher teacher = toeflService.getIdBytname(a);
				toefl.setPublish_teacher(teacher.getTeacher_id());
				toefl.setLast_modify(new Date());
				toeflService.editAtricle(toefl);
	        } else {
	        	String a  =(String) session.getAttribute("loginname");
				Teacher teacher = toeflService.getIdBytname(a);
				toefl.setPublish_teacher(teacher.getTeacher_id());
				toefl.setLast_modify(new Date());
				toeflService.editAtricle(toefl);

	        }
	        return "redirect:/toMyToefl";
	}

//	系统管理员-托福人跳转
	@RequestMapping("/toToeflAdmin")
	public String toToeflAdmin(Toefl toefl, HttpSession session,Model model,Integer pageNum) {
		
		
		if (pageNum != null) {
			toefl.setPageNum(pageNum);
//			全部列表
			PageInfo<Toefl> toefls=toeflService.schall(toefl);
			model.addAttribute("all", toefls);
//			搜索
			List<Teacher> teachers = toeflService.schTeachers();
			session.setAttribute("teacherList", teachers);
			List<ArticleType> articleTypes = toeflService.schArticleTypes();
			session.setAttribute("articleType", articleTypes);
			session.setAttribute("schArticle", toefl);
			return "toefl/toefl_admin";
		}else {
//			全部列表
			PageInfo<Toefl> toefls=toeflService.schall(toefl);
			model.addAttribute("all", toefls);
//			搜索
			List<Teacher> teachers = toeflService.schTeachers();
			session.setAttribute("teacherList", teachers);
			List<ArticleType> articleTypes = toeflService.schArticleTypes();
			session.setAttribute("articleType", articleTypes);
			session.setAttribute("schArticle", toefl);
			return "toefl/toefl_admin";
			}
	}

//	管理员-文章类别
	@RequestMapping("/article_class")
	public String article_class(Integer activity_type,HttpSession session,Model model,Integer pageNum,Toefl toefl) {
		if (pageNum != null) {
			toefl.setPageNum(pageNum);
			Integer a = (Integer) session.getAttribute("activity_type");
			PageInfo<Toefl> toefls = toeflService.articleClass(toefl,a);
			model.addAttribute("all", toefls);
			return "toefl/toefl_admin";
		}else {
			PageInfo<Toefl> toefls = toeflService.articleClass(toefl,activity_type);
			session.setAttribute("activity_type", activity_type);
			model.addAttribute("all", toefls);
			return "toefl/toefl_admin";
			}
	}
	
//	批量文章删除
	@RequestMapping("/delArticles")
	public String delArticles(Integer[] ids) {
		toeflService.delArticles(ids);
		return "redirect:/toToeflAdmin";
	}
	
//	禁读文章
	@RequestMapping("/banArticles")
	public String banArticles(Integer[] ids) {
		toeflService.banArticles(ids);
		return "redirect:/toToeflAdmin";
	}
	
//	解读文章
	@RequestMapping("/unlashArticles")
	public String unlashArticles(Integer[] ids) {
		toeflService.unlashArticles(ids);
		return "redirect:/toToeflAdmin";
	}
	
}
